﻿using System.Data;
using MySqlConnector;

namespace Front._1124;

public partial class AddBookForm1 : Form
{
    public AddBookForm1()
    {
        InitializeComponent();
    }



    private void AddBookForm1_Load(object sender, EventArgs e)
    {
        GetAllPublisher();
    }
    

    private void GetAllPublisher()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=root;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();

        string sql = "SELECT publisher_id,publisher_name FROM publisher_info;";
        //创建数据适配器
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        //创建数据集
        DataSet dataSet = new DataSet();
        //填充数据
        mySqlDataAdapter.Fill(dataSet, "publisher_info");
        //绑定数据
        DataTable publisherTable = dataSet.Tables["publisher_info"];
        //插入全部行
        DataRow row = publisherTable.NewRow();
        row[0] = 0;
        row[1] = "全部";
        publisherTable.Rows.InsertAt(row, 0);
        
        
        publisherId.DataSource=publisherTable;
        publisherId.ValueMember=publisherTable.Columns[0].ColumnName;
        publisherId.DisplayMember=publisherTable.Columns[1].ColumnName;
    }
//保存按钮
    private void button1_Click(object sender, EventArgs e)
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=root;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();

        string sql = "insert  into `book_info`(`isbn`,`book_name`,`author`,`publisher_id`,`publish_date`,`book_category`,`book_price`,`book_pages`,`book_desc`,`book_status`,`shelf_id`,`purchase_date`,`book_version`,`create_time`) values \n(@isbn,@bookName,@author,@publisherId,'2021-06-01','计算机科学',59.80,320,'讲解MySQL基础与实战开发',1,1,'2023-01-10','第2版','2025-10-10 10:11:29');";
        using var command = new MySqlCommand(sql, connection);
        command.Parameters.AddWithValue("isbn", this.isbn.Text);
        command.Parameters.AddWithValue("bookName", this.bookName.Text);
        command.Parameters.AddWithValue("publisherId", this.publisherId.SelectedValue);
        command.Parameters.AddWithValue("author", this.author.Text);
        int count = command.ExecuteNonQuery();
        if (count > 0)
        {
            MessageBox.Show("添加成功");
            this.DialogResult=DialogResult.OK;
            this.Close();
        }
    }
}